<?php

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 


/**
 *  @file
 *  @Author Samuel Maura
 *  Registration script
 */

require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/debug.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/controllers/validate.inc");
require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/lib/session/sessionUtils.inc");

//Named session

session_name('REGISTRATION');

// Create session
session_start();

//Copy post values

// save original values in session
savePost();

$_SESSION['validation_error'] = "";

$validatedInputs = array();

$inputs = trimInputArray($_POST);
$inputs = lowerCase($_POST);

/**
echo $inputs['email'];
echo 'Helo world';
die();
**/

validateAccountDetails($inputs['email'],
   $inputs['confirm_email'],
   $inputs['username'],
   $inputs['password'],
   $inputs['confirm_password']);

checkErrors('validation_error','/xenix/views/wikiregistration.php');

// Check again
 if ($_SESSION['validation_error'] == ''){
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/user.php");
    require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/sqlSafe.php");
    
    $user = new User();
    $sqlSafe = new SqlSafe(dbManager::connect());
    
    //var_dump($validatedInputs);
    
    foreach($validatedInputs as $key => $val){
      $validatedInputs[''.$key.''] = $sqlSafe->safe($val);
    }
    
     $validatedInputs['id'] = mt_rand(100000,999999).mt_rand(100000,999999);
     $validatedInputs['access'] = 1;
    
    // print_r($validatedInputs);
    
    if ($user->userExist($validatedInputs['username'])){
        $_SESSION['validation_error'] = "This username is already in use.";
        header("Location: /xenix/views/wikiregistration.php");
        exit();
    }
    
    if ($user->emailExist($validatedInputs['email'])){
        $_SESSION['validation_error'] = "This email address is already in use.";
        header("Location: /xenix/views/wikiregistration.php");
        exit();
    }
    
    
     $user->add($validatedInputs);
     //echo $user->userCount();
     header("Location: /xenix/views/wikilogin.php");
 }



//Validate input


function validateAccountDetails($email,$confirm_email,$username,$password,$confirm_password){
  $validate = new Validate();
  if ( isset($email) && ($email != '') ){
       if (!$validate->validEmail($email)){
         $_SESSION['validation_error'] .= "Email format invalid <br/>";
       }
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "Email address required <br/>";
  }
  
  
  if ( isset($confirm_email) && ($confirm_email != '') && ($confirm_email  == $email) ){
        $GLOBALS['validatedInputs']['email'] = $email;    
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "Email does not match confirm email address <br/>";
  }
  
  
  if ( isset($username) && ($username != '') ){
       if (!$validate->validUserName($username)){
         $_SESSION['validation_error'] .= "Username format invalid <br/>";
       }
       else{
          $GLOBALS['validatedInputs']['username'] = $username;   
       }
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "Username is required <br/>";
  }
  
  
  
  if ( isset($password) && ($password != '') ){
     if ( isset($confirm_password) && ($confirm_password != '') && ($confirm_password  == $password) ){
        $GLOBALS['validatedInputs']['password'] = $password;    
     }
     else{
      // set error message
      $_SESSION['validation_error'] .= "Password does not match confirm password field <br/>";
     }    
  }
  else{
    // set error message
    $_SESSION['validation_error'] .= "password required <br/>";
  }
  
} 




// Show debug info
//echo $GLOBALS['POST_INPUTS'];


?>